package com.itboye.lottery.activity.daletou

import android.content.Intent
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.text.Html
import android.view.View
import android.widget.TextView
import com.itboye.lottery.R
import com.itboye.lottery.activity.TouZhuListActivity
import com.itboye.lottery.adapter.CommonPagerAdapter
import com.itboye.lottery.alert.ByAlert
import com.itboye.lottery.base.BaseActivity
import com.itboye.lottery.bean.CodeBean
import com.itboye.lottery.fragment.home.shuangseqiu.DaleTouFragment
import com.itboye.lottery.utils.BetSelectMatches.randomCodeTotal
import com.itboye.lottery.utils.CodeUtil.randomCommon
import com.itboye.lottery.utils.Combination
import kotlinx.android.synthetic.main.activity_shuangseqiu_main.*
import kotlinx.android.synthetic.main.fragment_shuangseqiu.*
import java.util.*
import kotlin.collections.ArrayList

class DaLeTouMainActivity : BaseActivity() {


    internal var orderFragments: MutableList<Fragment>? = null

    override fun getLayoutId(): Int {
        return R.layout.activity_shuangseqiu_main
    }

    override fun initData() {
        initTitle()
        bindTl_Vp()
    }

    private fun initTitle() {
        setTitle("大乐透")
    }

    private var normalBet: DaleTouFragment? = null
    private var boldBet: DaleTouFragment? = null

    var selectionPosition: Int = 0

    companion object {
        var activity: DaLeTouMainActivity? = null
        var clear: Boolean = true
    }

    private fun bindTl_Vp() {
        orderFragments = ArrayList()
        normalBet = DaleTouFragment.newInstance(0)
        boldBet = DaleTouFragment.newInstance(1)
        orderFragments!!.add(normalBet!!)
        orderFragments!!.add(boldBet!!)
        vp_shuangseqiu.adapter = CommonPagerAdapter(this, orderFragments, R.array.shuangse_title, supportFragmentManager)
        vp_shuangseqiu.offscreenPageLimit = 5
        tb_shuangseqiu.setupWithViewPager(vp_shuangseqiu)
        vp_shuangseqiu.setCurrentItem(intent.getIntExtra("order_position", 0), false)
        vp_shuangseqiu.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                selectionPosition = position
            }

        })
    }

    var tv_jixuan: TextView? = null//机选
    var tv_bottom_clear: TextView? = null//清除已选
    var tv_bottom_sure: TextView? = null//投注
    var tv_bottom_heji: TextView? = null//合计/注数
    private var randomRedCode: ArrayList<CodeBean> = ArrayList()
    private var randomBlueCode: ArrayList<CodeBean> = ArrayList()
    private var randomDanTuoRedCode: ArrayList<CodeBean> = ArrayList()
    private var randomRedTuoMaCode: ArrayList<CodeBean> = ArrayList()
    private var randomDanBlueCode: ArrayList<CodeBean> = ArrayList()
    private var randomDanTuoBlueCode: ArrayList<CodeBean> = ArrayList()


    private var danTuoBetSelect: Boolean = true
    private var normalBetSelect: Boolean = true
    override fun onClick(p0: View?) {
        when (p0!!.id) {
            R.id.tv_jixuan -> {
                //机选
                //生成随机号码选中：
                when (selectionPosition) {
                    0 -> {
                        //生成红色号码
                        randomRedCode = randomCommon(1, 33, 5, 0)
                        //生成蓝色号码
                        randomBlueCode = randomCommon(1, 12, 2, 2)

                        normalBet!!.redAdapter!!.randomCodeUpdate(randomRedCode)
                        normalBet!!.blueAdapter!!.randomCodeUpdate(randomBlueCode)

                    }
                    1 -> {
                        //生成红色(胆)号码
                        randomDanTuoRedCode = randomCommon(1, 33, 4, 0)
                        //生成红色脱码（不能与胆码相同）
                        randomRedTuoMaCode = randomCommon(1, 33, 2, 1, randomDanTuoRedCode)
                        //生成蓝色号码
                        randomDanBlueCode = randomCommon(1, 12, 1, 2)
                        randomDanTuoBlueCode = randomCommon(1, 12, 2, 3, randomDanBlueCode)

                        boldBet!!.redAdapter!!.randomCodeUpdate(randomDanTuoRedCode)
                        boldBet!!.redTuoMaAdapter!!.randomCodeUpdate(randomRedTuoMaCode)
                        boldBet!!.blueAdapter!!.randomCodeUpdate(randomDanBlueCode)
                        boldBet!!.blueTuoMaAdapter!!.randomCodeUpdate(randomDanTuoBlueCode)
                    }
                    else -> {
                    }
                }
                setCalculateZhuShu(caculateZhuShu(selectionPosition))
            }
            R.id.tv_bottom_clear -> {
                //清除已选
                boldBet!!.blueAdapter!!.clearAllStatus()
                boldBet!!.redAdapter!!.clearAllStatus()
                boldBet!!.redTuoMaAdapter?.clearAllStatus()

                normalBet!!.redAdapter!!.clearAllStatus()
                normalBet!!.blueAdapter!!.clearAllStatus()
                setCalculateZhuShu(0)
            }
            R.id.tv_bottom_sure -> {
                //开始投注
                //遍历获取选中的号码(两个页面，如果有选，则要判断选中号码的个数贵则， 一个号码没选就视为不投注这种方式)
//                if (isNormalBetTrue()) {
//                randomCodeTotal?.clear()
                var map = HashMap<String, Any>()
                var endTime = ""
                when (selectionPosition) {
                    0 -> {
                        map = HashMap<String, Any>()
                        if (normalBet!!.redAdapter!!.selectCodeArray.size < 5) {
                            ByAlert.alert("红色前区至少选择5个")
                            return
                        }
                        map["red"] = normalBet!!.redAdapter!!.selectCodeArray
                        if (normalBet!!.blueAdapter!!.selectCodeArray.size < 2) {
                            ByAlert.alert("蓝色后区至少选择2个")
                            return
                        }
                        map["blue"] = normalBet!!.blueAdapter!!.selectCodeArray
                        map["zhushu"] = num
                        endTime = normalBet!!.tv_shuangseqiu_message_1!!.text.toString()
                    }
                    1 -> {
                        map = HashMap<String, Any>()
                        if (boldBet!!.redAdapter!!.selectCodeArray.size < 1) {
                            ByAlert.alert("红色胆码至少选择1个")
                            return
                        }

                        map["red"] = boldBet!!.redAdapter!!.selectCodeArray
                        if (boldBet!!.redTuoMaAdapter!!.selectCodeArray.size < 2) {
                            ByAlert.alert("红色拖码至少选择2个")
                            return
                        }
                        if (boldBet!!.redAdapter!!.selectCodeArray.size + boldBet!!.redTuoMaAdapter!!.selectCodeArray.size < 5) {
                            ByAlert.alert("红色胆码+红色拖码至少选择5个")
                            return
                        }
                        map["red_tuo"] = boldBet!!.redTuoMaAdapter!!.selectCodeArray
                        map["blue"] = boldBet!!.blueAdapter!!.selectCodeArray
                        if (boldBet!!.blueTuoMaAdapter!!.selectCodeArray.size < 2) {
                            ByAlert.alert("蓝色拖码至少选择2个")
                            return
                        }
                        map["blue_tuo"] = boldBet!!.blueTuoMaAdapter!!.selectCodeArray
                        map["zhushu"] = num
                        endTime = boldBet!!.tv_shuangseqiu_message_1!!.text.toString()
                    }
                }
                randomCodeTotal.add(map)
//                if (normalBetSelect) {
//
//                }
//                if (danTuoBetSelect) {
//
//                }

                if (TouZhuListActivity.activity != null) {
                    TouZhuListActivity.activity.finish()
                }
                startActivity(Intent(this, TouZhuListActivity::class.java)
                        .putExtra("arrayCode", randomCodeTotal)//所有号码
                        .putExtra("end_time",endTime)
                        .putExtra("type", false))//倍数
            }

        }
    }

    override fun onDestroy() {
        super.onDestroy()
        randomCodeTotal?.clear()
    }

    fun caculateZhuShu(selectionPosition: Int): Int {
        //计算注数
        when (selectionPosition) {
            0 -> {
                var num = Combination.findsort(randomRedCode, 5)
                var num2 = Combination.findsort(randomBlueCode, 2)
                return num * num2
            }
            else -> {
                var num = Combination.findsort(randomDanTuoRedCode, 4)
                var num2 = Combination.findsort(randomDanBlueCode, 1)
                var num3 = Combination.findsort(randomRedTuoMaCode, 5 - randomDanTuoRedCode.size)
                var num4 = Combination.findsort(randomDanTuoBlueCode, 2 - randomDanBlueCode.size)
                return num * num2 * num3 * num4
            }
//            0 -> {
//                var num = Combination.findsort(randomRedCode, 5).size
//                var num2 = Combination.findsort(randomBlueCode, 2).size
//                return num * num2
//            }
//            else -> {
//                var num = Combination.findsort(randomDanTuoRedCode, 4).size
//                var num2 = Combination.findsort(randomDanBlueCode, 1).size
//                var num3 = Combination.findsort(randomRedTuoMaCode, 5 - randomDanTuoRedCode.size).size
//                var num4 = Combination.findsort(randomDanTuoBlueCode, 2 - randomDanBlueCode.size).size
//                return num * num2 * num3 * num4
//            }
        }

    }

    var num = 0
    fun setCalculateZhuShu(num: Int) {
        this.num = num
        tv_bottom_heji!!.text = Html.fromHtml("<font color='red'>${num * 2}元</font><br/>${num}注")
    }

}

